﻿using System;
using System.Collections.Generic;
using System.Text;
using AdaptiveAgents.Tools;
using System.Data;

namespace AdaptiveAgents.Formulas
{
    class SecondPartOfCheckingTool
    {
        public SecondPartOfCheckingTool() { }

        /*private double[] sortArray1(double[] vec,Record rec)
        {
            double[] returnVec = new double[vec.Length];
            int ag0 = DoubleComperator.getPosition(rec.getCompetence(0),rec.getCompetence(1),rec.getCompetence(2));
            int ag1 = DoubleComperator.getPosition(rec.getCompetence(1),rec.getCompetence(0),rec.getCompetence(2));
            int ag2 = DoubleComperator.getPosition(rec.getCompetence(2), rec.getCompetence(0), rec.getCompetence(1));

            double bigger = DoubleComperator.getBiggest(vec[0], vec[1], vec[2]);
            double smaller = DoubleComperator.getSmallest(vec[0], vec[1], vec[2]);
            double middle = DoubleComperator.getMiddle(vec[0], vec[1], vec[2]);

            match(returnVec, 0, ag0, bigger, smaller, middle);
            match(returnVec, 1, ag1, bigger, smaller, middle);
            match(returnVec, 2, ag2, bigger, smaller, middle);

            

            return returnVec;
        }*/
        
        public double[] solveRecord(ProbabilitiesMatrix matrix,Record rec)
        {
                        
            double[] EquationVec0 = new double[4];
            double[] EquationVec1 = new double[4];
            double[] EquationVec2 = new double[4];
            double[] EquationVec3 = new double[4];

            double[] cloneVec0 = null;
            double[] cloneVec1 = null;
            double[] cloneVec2 = null;
            double[] cloneVec3 = null;

            double[] returnVec0 = null;
            double[] returnVec1 = null;
            double[] returnVec2 = null;

            EquationSolver.EquationSolver solver = new EquationSolver.EquationSolver();
            
                        
            EquationVec0[0] = (-1);
            EquationVec0[1] = (-1);
            EquationVec0[2] = (-1);
            EquationVec0[3] = (-1);

            EquationVec1[0] = (matrix.getProbability0to0() - 1);
            EquationVec1[1] = (matrix.getProbability1to0());
            EquationVec1[2] = (matrix.getProbability2to0());
            EquationVec1[3] = (0);

            EquationVec2[0] = (matrix.getProbability0to1());
            EquationVec2[1] = (matrix.getProbability1to1() - 1);
            EquationVec2[2] = (matrix.getProbability2to1());
            EquationVec2[3] = (0);

            EquationVec3[0] = (matrix.getProbability0to2());
            EquationVec3[1] = (matrix.getProbability1to2());
            EquationVec3[2] = (matrix.getProbability2to2() - 1);
            EquationVec3[3] = (0);
            
            cloneVec0 = (double[])EquationVec0.Clone();
            cloneVec1 = (double[])EquationVec1.Clone();
            cloneVec2 = (double[])EquationVec2.Clone();
            cloneVec3 = (double[])EquationVec3.Clone();

            
            solver.addEquation(EquationVec1);
            solver.addEquation(EquationVec2);
            solver.addEquation(EquationVec0);           
            returnVec0 = solver.solve();

            EquationVec0 = (double[])cloneVec0.Clone();
            EquationVec1 = (double[])cloneVec1.Clone();
            EquationVec3 = (double[])cloneVec3.Clone();
            solver.clear();
            solver.addEquation(EquationVec0);
            solver.addEquation(EquationVec1);
            solver.addEquation(EquationVec3);
            returnVec1 = solver.solve();

            EquationVec0 = (double[])cloneVec0.Clone();
            EquationVec3 = (double[])cloneVec2.Clone();
            EquationVec3 = (double[])cloneVec3.Clone();
            solver.clear();
            solver.addEquation(EquationVec0);
            solver.addEquation(EquationVec2);
            solver.addEquation(EquationVec3);
            returnVec2 = solver.solve();

           
            if (returnVec0[0] + returnVec0[1] + returnVec0[2] == 1)
            {
                return returnVec0;
            }
            else if (returnVec1[0] + returnVec1[1] + returnVec1[2] == 1)
            {
                return returnVec1;
            }
            else
            {
                return returnVec2;                
            }
        }

        /*private void match1(double[] vec, int index, int level, double big, double small, double mid)
        {
            if (level == 0)
                vec[index] = small;
            else if (level == 1)
                vec[index] = mid;
            else
                vec[index] = big;
        }*/


    }
}
